package com.victor.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.group.GroupCollection;
import com.victor.entity.group.GroupTour;
import com.victor.entity.group.GroupTour_Signup;
import com.victor.service.GroupService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

/**
 * 团游--app
 * 
 *
 */
@RestController
@RequestMapping("/groupapp")
public class GroupAppController {

	@Autowired
	private GroupService service; 
	
	/**
	 * 团游列表
	 * @param city
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/tourlsit")
	public Msg findGroupTour(String city,String classid,int page,int row) {
		if (city == null || city.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<GroupTour> selGroupByCity = service.selGroupByCity(city,classid,page,row);
		return Msg.success().add("grouptour", selGroupByCity);
	}
	/**
	 * 添加团游报名
	 * @param signup
	 * @return
	 */
	@RequestMapping("addsign")
	public Msg addGroupSign(GroupTour_Signup signup) {
		String[] str = {"grouptourid","uid","uname","uphone","personnum"};
		boolean empty = ClassFilter.somePropertyIsEmpty(signup, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean insertSign = service.insertSign(signup);
		GroupTour selByGroupId = service.selByGroupId(Integer.valueOf(signup.getGrouptourid()));
		if (Integer.valueOf(selByGroupId.getTotalnum()) < Integer.valueOf(signup.getPersonnum())) {
			return Msg.fail("人数剩余不足。。。");
		}
		boolean b = service.updatePerson(signup.getPersonnum(),signup.getGrouptourid());
		if (!b) {
			return Msg.fail("修改团游人数失败");
		}
		return Msg.judge(insertSign);
		
	}
	/**
	 * 团游详情
	 * @param groupid
	 * @param uid
	 * @return
	 */
	@RequestMapping("/groupdetails")
	public Msg groupXiangqing(Integer groupid,String uid) {
		if (groupid == null) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			GroupTour selByGroupId = service.selByGroupId(groupid);
			return Msg.success().add("info", selByGroupId);
		}else {
			GroupTour selByGroupIdAndUid = service.selByGroupIdAndUid(groupid,uid);
			return Msg.success().add("info", selByGroupIdAndUid);
		}
	}
	/**
	 * 收藏团游
	 * @param uid
	 * @param groupid
	 * @return
	 */
	@RequestMapping("/collectiongroup")
	public Msg groupCollection(GroupCollection controller) {
		String[] strings = {"grouptourid","uid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(controller, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		boolean b = service.insertCollction(controller);
		return Msg.judge(b);
		
	}
}
